/*
 * CheckBoxMenuItem.java  0.4.0 / Oct 25, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing;

import com.cute4j.swing.events.ActionEvent;
import com.cute4j.swing.properties.CheckBoxMenuItemProperties;

/**
 * CheckBox Menu Item.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class CheckBoxMenuItem extends MenuItem
                              implements CheckBoxMenuItemProperties {

// Static Initialization. ------------------------------------------------------

  static {
    _initialize();
  }

// Private Fields. -------------------------------------------------------------

  private boolean checked;

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new CheckBoxMenuItem object.
   */
  public CheckBoxMenuItem() {
    // empty constructor body
  }

  /**
   * Constructs new CheckBoxMenuItem object.
   *
   * @param text the text
   */
  public CheckBoxMenuItem(String text) {
    super(text);
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns <tt>true</tt> if menu item is checked.
   *
   * @return <tt>true</tt> if menu item is checked
   */
  public boolean isChecked() {
    return checked;
  }

  /**
   * Checks/unchecks menu item.
   *
   * @param checked if <tt>true</tt> menu item is checked
   */
  public void setChecked(boolean checked) {
    if (this.checked != checked) {
      disposedCheck();
      _setChecked(_delegate, checked);
    }
  }

  /**
   * Returns selected icon.
   *
   * @return selected icon
   */
  public Icon getSelectedIcon() {
    if (iconSet != null) {
      return iconSet.getSelectedIcon();
    }
    return null;
  }

  /**
   * Sets selected icon.
   *
   * @param icon the selected icon
   */
  public void setSelectedIcon(Icon icon) {
    disposedCheck();
    if (icon == null) {
      if (iconSet != null) {
        iconSet.setSelectedIcon(null, null);
        if (iconSet.isEmpty()) {
          _setIcon(_delegate, null);
          iconSet = null;
        }
      }
    } else {
      if (iconSet == null) {
        iconSet = new SwingIconSet(this);
        _setIcon(_delegate, iconSet);
      }
      iconSet.setSelectedIcon(icon, null);
    }
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  protected String paramString() {
    StringBuffer sb = new StringBuffer(super.paramString());
    if (checked) {
      sb.append(",checked");
    }
    return sb.toString();
  }

// Native Callbacks. -----------------------------------------------------------

  void _checkedChanged(int modifiers, boolean checked) {
    final boolean oldChecked = this.checked;
    if (oldChecked != checked) {
      this.checked = checked;
      firePropertyChange(PROPERTY_CHECKED, oldChecked, checked);
      fireActionPerformed(new ActionEvent(this, ActionEvent.ACTION_PERFORMED,
                                          getActionCommand(),
                                          System.currentTimeMillis(),
                                          modifiers));
    }
  }

// Package-Private Native Methods. ---------------------------------------------

  native long _create();

  native void _setText(long delegate, String text);

  native void _setShortcut(long delegate, String shortcut);

  native void _setIcon(long delegate, SwingIconSet iconSet);

  native void _setChecked(long delegate, boolean checked);

// Private Static Native Method. -----------------------------------------------

  private static native void _initialize();

}
